package com.renwei.yunlong.activity.news;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.PersonSelectActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.ReturnBean;
import com.renwei.yunlong.event.ComplainPageRefreshEvent;
import com.renwei.yunlong.event.ProblemPageRefreshEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 指派给--页面
 * <p>
 * Created by Administrator on 2017/8/28.
 */

public class ProblemAssignActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_handling)
    TextView tvHandling;
    @Bind(R.id.rl_handling)
    RelativeLayout rlHandling;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    @Bind(R.id.btn_deal)
    Button btnDeal;
    private String questionId;
    private String newequestId;
    private String employeeId;
    private String classification;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_problem_sent);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        questionId = getIntent().getStringExtra("questionId");
        classification = getIntent().getStringExtra("classification");
        newequestId = getIntent().getStringExtra("newequestId");
        initData();
    }

    private void initData() {
        simpleTileView.setTitle("指派给");

        rlHandling.setOnClickListener(this);
        btnDeal.setOnClickListener(this);
        tvInputTitle.setText("指派意见(必填)");
        etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        etInputContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = s.length();
                tvInputNumber.setText(length + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_handling:
                if ("问题指派".equals(classification)) {
                    PersonSelectActivity.OpenActivity(mContext, PersonSelectActivity.FROM_COMPLAIN_ASSIGN, 111);
                } else if ("投诉指派".equals(classification)) {
                    PersonSelectActivity.OpenActivity(mContext, PersonSelectActivity.FROM_PROBLEM_ASSIGN, 111);
                }
                break;
            case R.id.btn_deal:
                String result = etInputContent.getText().toString().trim();
                if (TextUtils.isEmpty(employeeId)) {
                    showCenterInfoMsg("请选择处理人");
                    return;
                }
                if (TextUtils.isEmpty(result)) {
                    showCenterInfoMsg("请输入指派意见");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                if ("问题指派".equals(classification)) {
                    if ("1".equals(companyType)) {
                        map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                    } else if ("2".equals(companyType)) {
                        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                    }
                    map.put("responseUserId", employeeId);
                    map.put("questionId", questionId);
                    map.put("result", result);
                    map.put("flag", "1");
                    ServiceRequestManager.getManager().setProblemAssign(this, JsonMapUtil.mapToJson(map), this);
                } else if ("投诉指派".equals(classification)) {
                    if ("1".equals(companyType)) {
                        map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                    } else if ("2".equals(companyType)) {
                        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                    }
                    map.put("userids", employeeId);
                    map.put("complainIds", questionId);
                    map.put("newRequestId", newequestId);
                    map.put("cause", result);
                    ServiceRequestManager.getManager().setComplainAssign(this, JsonMapUtil.mapToJson(map), this);
                }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            if (requestCode == 111) {
                tvHandling.setText(bundle.getString("name"));
                employeeId = bundle.getString("employeeId");
            }
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                ReturnBean problemBean = new Gson().fromJson(result, ReturnBean.class);
                if (problemBean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    finish();
                } else if (problemBean.getMessage().getCode() == 1008) {
                    showCenterInfoMsg("无权限访问");
                } else {
                    showCenterInfoMsg(problemBean.getMessage().getMessage());
                }
                EventBus.getDefault().post(new ProblemPageRefreshEvent());
                break;
            case 2:
                ReturnBean ComplainBean = new Gson().fromJson(result, ReturnBean.class);
                if (ComplainBean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    finish();
                } else if (ComplainBean.getMessage().getCode() == 1008) {
                    showCenterInfoMsg("无权限访问");
                } else {
                    showCenterInfoMsg(ComplainBean.getMessage().getMessage());
                }
                EventBus.getDefault().post(new ComplainPageRefreshEvent());
                break;
                default:
                break;
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Context context, String classification, String questionId, String newequestId) {
        Intent intent = new Intent(context, ProblemAssignActivity.class);
        intent.putExtra("classification", classification);
        intent.putExtra("questionId", questionId);
        intent.putExtra("newequestId", newequestId);
        context.startActivity(intent);
    }
}
